package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * description: 
 * @author zhufeng
 * @date 2024/4/8 0008 10:48
 * @version V1.0
 */
public class 使数组连续的最少操作数_2009 {
    public int minOperations(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        List<Integer> list = new ArrayList<>(set);
        Collections.sort(list);

        int left = 0;
        int right = left+1;
        int max = 1;
        int temp = 1;
        while (right < list.size()) {
            if (Math.abs(list.get(left) - list.get(right)) < nums.length) {
                right++;
                temp++;
            } else {
                left++;
                temp--;
            }
            max = Math.max(max, temp);
        }
        return nums.length-max;
    }

    @Test
    public void testMinOperations () {
        int[] nums = {10,49,16,22,28,34,35,7};
        System.out.println(minOperations(nums));
    }
}
